<template>
    <div class="container seckillTkShop">
        <!-- header -->
        <div class="header">
            <div class="header-box">
                <p class="header-title">商品名称:</p>
                <el-input
                    v-model="header.goods_title"
                    placeholder="请输入商品名称"
                    suffix-icon="el-icon-search"
                    size="small"
                    clearable
                    @change="getTableData(1)"
                    @keyup.native.enter="getTableData(1)"
                />
            </div>
            <div class="header-box">
                <p class="header-title">店铺名称:</p>
                <el-input
                    v-model="header.shop_title"
                    placeholder="请输入店铺名称"
                    suffix-icon="el-icon-search"
                    size="small"
                    clearable
                    @change="getTableData(1)"
                    @keyup.native.enter="getTableData(1)"
                />
            </div>
            <div class="header-box">
                <p class="header-title">上架状态:</p>
                <el-select
                    v-model="header.goods_status"
                    clearable
                    size="small"
                    placeholder="请选择"
                    @change="getTableData(1)"
                >
                    <el-option
                        v-for="item in upperList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
            </div>
            <div class="header-box">
                <p class="header-title">是否设置库存:</p>
                <el-select
                    v-model="header.stock"
                    clearable
                    size="small"
                    placeholder="请选择"
                    @change="getTableData(1)"
                >
                    <el-option
                        v-for="item in stockList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
            </div>
            <div class="header-box">
                <p class="header-title">淘礼金面额/佣金:</p>
                <el-input
                    v-model="header.start_cash_gift"
                    class="search-ipt"
                    size="small"
                    type="number"
                    min="0"
                />
                <p class="header-price">-</p>
                <el-input
                    v-model="header.end_cash_gift"
                    size="small"
                    class="search-ipt"
                    type="number"
                    min="0"
                />
            </div>
            <div class="header-box">
                <p class="header-title">秒杀价:</p>
                <el-input
                    v-model="header.start_amount"
                    class="search-ipt"
                    size="small"
                    type="number"
                    min="0"
                />
                <p class="header-price">-</p>
                <el-input
                    v-model="header.end_amount"
                    size="small"
                    class="search-ipt"
                    type="number"
                    min="0"
                />
            </div>
            <div class="header-box">
                <el-button
                    type="primary"
                    size="small"
                    @click="fnReset()"
                >
                    重置
                </el-button>
            </div>
            <div class="header-box">
                <el-button
                    type="primary"
                    size="small"
                    @click="getTableData(1)"
                >
                    搜索
                </el-button>
            </div>
            <div class="header-box">
                <el-button
                    type="primary"
                    size="small"
                    @click="fnImportTk()"
                >
                    导入淘客
                </el-button>
            </div>
            <div class="header-box">
                <el-button
                    type="primary"
                    size="small"
                    @click="fnLookDetails()"
                >
                    查看每日淘礼金
                </el-button>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="zeroPurchase"
            @sort-change="handleSortChanged"
            @select="fnSelect"
            @select-all="fnSelectAllTop"
            @selection-change="handleSelectionChange"
        >
            <el-table-column
                type="selection"
                width="40"
                fixed="left"
            />
            <el-table-column
                prop="goods_title"
                fixed="left"
                label="商品名称"
                show-overflow-tooltip
                :width="140"
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="goods_cover"
                min-width="110"
                label="商品缩略图"
            >
                <template slot-scope="scope">
                    <img
                        :src="scope.row.goods_cover"
                        class="img-cover"
                    >
                </template>
            </el-table-column>
            <el-table-column
                prop="goods_no"
                label="商品编号"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="shop.shop_title"
                label="店铺名称"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="goods_parent_type_name"
                label="商品分类"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="goods_type_name"
                label="二级分类"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="goods_price"
                label="淘宝售价"
                show-overflow-tooltip
                :width="120"
                :formatter="emptyFormatter"
                sortable="custom"
                :render-header="renderHeader"
            />
            <el-table-column
                prop="goods_coupon_money"
                label="优惠券"
                :width="110"
                show-overflow-tooltip
                sortable="custom"
            />
            <el-table-column
                prop="goods_price_pay"
                label="券后价"
                :width="100"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                sortable="custom"
                :render-header="renderHeaderPay"
            >
                <template slot-scope="scope">
                    {{ scope.row.goods_price_pay | setMoney }}
                </template>
            </el-table-column>
            <el-table-column
                prop="goods_commission"
                label="佣金"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                sortable="custom"
            />
            <el-table-column
                prop="ms"
                label="秒杀价"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                sortable="custom"
                :width="120"
                :render-header="renderHeaderMs"
            >
                <template slot-scope="scope">
                    {{ scope.row.goods_price_pay - scope.row.goods_commission | setMoney }}
                </template>
            </el-table-column>
            <el-table-column
                prop="goods_commission"
                label="淘礼金面额"
                :width="130"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                sortable="custom"
                :render-header="renderHeaderComin"
            >
                <template slot-scope="scope">
                    {{ scope.row.goods_commission | setMoney }}
                </template>
            </el-table-column>
            <el-table-column
                prop="sale_num"
                label="商品销量"
                :width="110"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                sortable="custom"
            >
                <template slot-scope="scope">
                    <span
                        @click="fnOrderList(scope.row)"
                        class="curse-add"
                    >{{ scope.row.sale_num }}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="consume_money"
                label="已消费淘礼金"
                :width="140"
                show-overflow-tooltip
                sortable="custom"
            />
            <el-table-column
                prop="join_time"
                label="添加时间"
                align="center"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                sortable
                :width="160"
            >
                <template slot-scope="scope">
                    {{ scope.row.join_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                </template>
            </el-table-column>
            <el-table-column
                prop="goods_status"
                label="上架状态"
            >
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.goods_status"
                        :active-value="1"
                        :inactive-value="0"
                    />
                    <p
                        class="switch-fixed"
                    />
                </template>
            </el-table-column>
            <el-table-column
                prop="stock"
                label="起始库存"
                :width="120"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="goods_buy_limit_count"
                label="限购次数"
                width="110px"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    {{ scope.row.goods_buy_limit_count == -1 ? '不限制' : scope.row.goods_buy_limit_count }}
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                fixed="right"
                :width="160"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnRemoveShop(scope.row)"
                    >
                        移除
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnSetStock(scope.row)"
                    >
                        设置起始库存
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- 淘礼金弹窗 -->
        <el-dialog
            :visible.sync="dialogVisibleList"
            width="660px"
            :close-on-click-modal="false"
        >
            <div class="main">
                <p class="main-text">已用淘礼金总额：{{ cashCount }}</p>
                <i
                    class="el-icon-refresh-right main-icon"
                    @click="fnRefresh()"
                />
            </div>
            <el-table
                :data="tableDateResh"
                height="100%"
                style="width: 100%;"
                border
            >
                <el-table-column
                    prop=""
                    label="时间"
                    align="center"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        {{ scope.row.day_time * 1000 | dateFormatter('yyyy/MM/dd') }}
                    </template>
                </el-table-column>
                <el-table-column
                    prop="consume_money"
                    label="已用淘礼金"
                    align="center"
                    show-overflow-tooltip
                />
            </el-table>
        </el-dialog>
        <!-- /淘礼金弹窗 -->

        <!-- 导入淘客 -->
        <el-dialog
            title="导入客户淘客"
            width="530px"
            custom-class="schedule-dialog"
            :visible.sync="showAddDialog"
        >
            <el-form
                :model="ruleForm"
                :rules="rules"
                ref="ruleForm"
                class="demo-ruleForm"
                label-width="100px"
            >
                <el-form-item
                    label="店铺名称"
                >
                    <el-select
                        v-model="ruleForm.shop_no"
                        filterable
                        remote
                        placeholder="请输入客户的店铺名称"
                        :remote-method="fnSearchShop"
                        @change="handleSelect"
                        :loading="loading"
                        size="small"
                        style="width:315px"
                    >
                        <el-option
                            v-for="item in shopListChoose"
                            :key="item.shop_no"
                            :label="item.shop_title"
                            :value="item.shop_no"/>
                    </el-select>
                    <span :style="{'margin': '0px 5px 0 5px'}">(选填)</span>
                    <el-popover
                        placement="bottom-start"
                        width="280"
                        trigger="hover"
                        content="导入客户商品时，如该客户已有店铺，则需导入至该客户的已有店铺中去，以防出现多个店铺的情况">
                        <el-button
                            slot="reference"
                            type="text"
                        >
                            <span
                                class="iconfont el-icon-hand-ziyuan"
                                :style="{'color': '#606266'}"
                            />
                        </el-button>
                    </el-popover>
                </el-form-item>
                <el-form-item
                    label="商品链接"
                    prop="url"
                >
                    <el-input
                        type="textarea"
                        :autosize="{ minRows: 2, maxRows: 6}"
                        v-model="ruleForm.url"
                        placeholder="请输入商品链接"
                    />
                </el-form-item>
                <el-form-item
                    label="商品分类"
                >
                    <el-cascader
                        placeholder="请选择商品分类"
                        :options="typeAllList"
                        filterable
                        clearable
                        size="small"
                        expand-trigger="click"
                        v-model="ruleForm.fication"
                        :props="{
                            label: 'title',
                            value: 'no',
                            children: 'children'
                        }"
                        style="width: 350px"
                    />
                    <span>(选填)</span>
                </el-form-item>
            </el-form>
            <div
                slot="footer"
                class="dialog-footer"
            >
                <el-button
                    size="small"
                    type="primary"
                    @click="submitForm('ruleForm')">提交</el-button>
            </div>
        </el-dialog>
        <!-- /导入淘客 -->

        <!-- 排入秒杀 -->
        <el-dialog
            :visible.sync="seckillDialogVisible"
            width="430px"
            center
        >
            <el-form
                :model="ruleFormMs"
                :rules="rulesMs"
                ref="ruleFormMs"
                label-width="100px"
                class="demo-ruleForm"
            >
                <el-form-item
                    label="选择日期"
                    prop="scekillData"
                >
                    <el-date-picker
                        v-model="ruleFormMs.scekillData"
                        type="date"
                        size="small"
                        placeholder="选择秒杀日期"
                        @change="fnGetSckillDate"
                        style="width:100%"
                    />
                </el-form-item>
                <el-form-item
                    label="选择时间"
                    prop="scekillTime"
                >
                    <el-select
                        v-model="ruleFormMs.scekillTime"
                        placeholder="选择秒杀时间"
                        size="small"
                        style="width:100%"
                    >
                        <el-option
                            v-for="(item,index) in scekillTimeList"
                            :key="index"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button
                        size="small"
                        class="scekillrel"
                        @click="resetForm('ruleFormMs')">取消</el-button>
                    <el-button
                        type="primary"
                        size="small"
                        class="scekillbtn"
                        @click="submitFormMs('ruleFormMs')">确定</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
        <!-- /排入秒杀 -->

        <!-- /分页 -->
        <div class="pagination-wrapper clearfix">
            <el-checkbox
                v-model="checkedAll"
                :indeterminate="checkIndeterminate"
                @change="fnChangeChecked()"
            >全选</el-checkbox>
            <el-button
                type="text"
                @click="fnBatchRemove()"
                :disabled="multipleSelection.length == 0 ? true : false"
            >批量移除</el-button>
            <el-button
                type="text"
                :disabled="multipleSelection.length == 0 ? true : false"
                @click="fnDischargeScekill()"
            >排入秒杀</el-button>
            <el-button
                type="text"
                :disabled="multipleSelection.length == 0 ? true : false"
                @click="fnBatchSetstock()"
            >批量设置库存</el-button>
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>
    </div>
</template>

<script>
import { setDate } from '@/assets/js/utils';
export default {
    /**
     * @param  {Object}    authResult           //表单权限
     * @param  {Array}     tableData            //表单数据
     * @param  {Object}    page                 //表单分页
     * @param  {Object}    header               //表单检索字段
     * @param  {Array}     upperList            //表单上架状态
     * @param  {Array}     isShop               //商品显示状态
     * @param  {Array}     isCoupon             //是否设置学生券
     * @param  {Boolean}   checkedAll           //全选状态
     * @param  {Array}     multipleSelection    //全选数据
     * @param  {Boolean}   checkIndeterminate   //全选状态
     * @param  {String}    cash_gift            //已消费淘礼金
     * @param  {Array}     tableDataCash        //淘礼金弹窗数据
     * @param  {Array}     dialogVisibleList    //淘礼开关
     * @param  {String}    cashCount            //已用淘礼金总额
     * @param  {Boolean}   dialogVisibleCoup    //设置学生券弹窗开关
     * @param  {Number}    coupon_num           //设置学生券数量
     * @param  {Object}    filterData           //排序
     * @param  {Number}    indexError           //批量移除成功数量
     * @param  {Number}    indexSucess          //排序移除失败数量
     * @param  {String}    goods_no             //商品编号
     * @param  {Array}     tableDateResh        //淘礼金弹窗数据
     */
    data () {
        return {
            scekillTimeList: [],
            ruleFormMs: {
                scekillData: '',
                scekillTime: '',
            },
            rulesMs: {
                scekillData: [
                    { required: true, message: '请选择日期', trigger: 'blur' }
                ],
                scekillTime: [
                    { required: true, message: '请选择时间', trigger: 'blur' }
                ]
            },
            seckillDialogVisible: false,
            nowPage: 1,
            typeList: [],
            shopListChoose: [],
            showAddDialog: false,
            ruleForm: {
                shop_no: '',
                url: '',
                fication: ''
            },
            rules: {
                url: [
                    {required: true, message: '请输入商品链接', trigger: 'blur'}
                ],
                // shop_no: [
                //     {required: true, message: '请输入客户的店铺名称', trigger: 'blur'}
                // ],
                // fication: [
                //     {required: true, message: '请选择商品分类', trigger: 'blur'}
                // ]
            },
            goods_no: '',
            indexSucess: 0,
            indexError: 0,
            coupon_num: 1,
            dialogVisibleCoup: false,
            cashCount: '0.00',
            dialogVisibleList: false,
            tableDataCash: [],
            tableDateResh: [],
            cash_gift: '0.00',
            checkIndeterminate: false,
            multipleSelection: [],
            checkedAll: false,
            loading: false,
            stockList: [{
                value: 99,
                label: '全部'
            },{
                value: 1,
                label: '是'
            },{
                value: 0,
                label: '否'
            }],
            upperList: [{
                value: 99,
                label: '全部'
            },{
                value: 1,
                label: '已上架'
            },{
                value: 0,
                label: '已下架'
            }],
            header: {
                stock: 99,
                goods_title: '',
                shop_title: '',
                goods_status: 99,
                start_amount: '',
                end_amount: '',
                start_cash_gift: '',
                end_cash_gift: ''
            },
            filterData: {
                sort_field: 0,
                sort_type: 0
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: [],
            authResult: {}
        };
    },
    filters: {
        setMoney (val) {
            if (val === null || val == 0) return '0.00';
            return Number(val).toFixed(2);
        }
    },
    computed: {
        typeAllList(){
            return [].concat(this.typeList);
        },
    },
    watch: {
        showAddDialog (val) {
            if (!val) {
                this.ruleForm = {
                    shop_no: '',
                    url: '',
                    fication: ''
                };
            }
        },
        dialogVisibleList (val) {
            if (!val) {
                this.tableDateResh = this.tableDataCash[0];
            }
        },
        seckillDialogVisible (val) {
            if (!val) {
                this.$refs['ruleFormMs'].resetFields();
                this.scekillTimeList = [];
            }
        }
    },
    created () {
        this.authResult = this.$route.meta.authResult;
        this.setHeader();
        this.getTableData();
        this.getLookDetails();
        this.fnGetGoodsType();

        // this.tableData = [{
        //     goods_price: 10,
        //     join_time: 1577126811,
        //     goods_parent_type_name: '一级分类',
        //     goods_type_name: '二级分类',
        //     goods_price_pay: 8,
        //     goods_no: 'sp_qs12992',
        //     goods_cover: 'https://img.alicdn.com/bao/uploaded/i2/1775517593/O1CN01XHFUke25xdsv2g4Pf_!!0-item_pic.jpg',
        //     sale_num: 10001,
        //     goods_title: '女装🐷🐶🐱🐑😄',
        //     goods_coupon_money: 2,
        //     goods_commission: 2,
        //     shop: {
        //         shop_no: 'go_s2133',
        //         shop_title: '一梦女装店'
        //     },
        //     stock: 0,
        //     goods_status: 1,
        //     consume_money: 0.5
        // }];
    },
    methods: {
        /**
         * 设置表单header
         */
        setHeader () {
            this.$store.commit('SET_HEADER', [{title: "APP后台",},{title: '商城'},{title: "秒杀"},{title: "秒杀淘客商品"}]);
        },

        /** 商品二级分类 */
        fnGetGoodsType(){
            this.$post('/student/shop_goods_type%5Cget_list_linkage', {}, resp => {
                if(resp.code == 1){
                    this.typeList = resp.data.list;
                }
            });
        },

        /**
         * 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/shop_goods_flash_sale%5Cget_tao_item_list', {
                page_id: page || this.page.now_page,
                sort_field: this.filterData.sort_field,
                sort_type: this.filterData.sort_type,
                goods_title: this.header.goods_title,
                shop_title: this.header.shop_title,
                goods_status: this.header.goods_status,
                has_set_stock: this.header.stock,
                goods_commission_range: this.header.start_cash_gift + '-' + this.header.end_cash_gift,
                sale_buy_money_range: this.header.start_amount + '-' + this.header.end_amount
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 跳转订单页面
         */
        fnOrderList (row) {
            this.$router.replace({
                name: 'OrderList',
                query: {
                    shop_type: '2',
                    goods_keyword: row.goods_no
                }
            });
        },

        /**
         * 重置
         */
        fnReset () {
            this.header = {
                stock: 99,
                goods_title: '',
                shop_title: '',
                goods_status: 99,
                start_amount: '',
                end_amount: '',
                start_cash_gift: '',
                end_cash_gift: ''
            };
            this.getTableData(1);
        },

        /**
         * 导入淘客
         */
        fnImportTk (event) {
            this.showAddDialog = !this.showAddDialog;
        },

        /**
         * 导入淘客数据
         */
        submitForm (formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.$post('/student/shop_goods_flash_sale%5Cimport_tao_item', {
                        content: this.ruleForm.url,
                        shop_no: this.ruleForm.shop_no,
                        type_no: this.ruleForm.fication[1]
                    }, resp => {
                        this.showAddDialog = !this.showAddDialog;
                        if (resp.code == 1) {
                            this.getTableData(1);
                            this.$message({
                                type: 'success',
                                message: '导入成功'
                            });
                        } else if (resp.code == 0) {
                            this.$message({
                                type: 'warning',
                                message: resp.msg
                            });
                        }
                    });
                }
            });
        },

        /**
         * 导入淘客
         */
        handleSelectLabel(item) {
            this.ruleFormLabel.label = item;
        },

        /**
         * 店铺搜索
         */
        fnSearchShop(word) {
            if(word != '') {
                this.loading = true;
                this.$post('/student/shop%5Csuggest', {
                    keyword: word
                }, resp => {
                    if (resp.code == 1) {
                        this.shopListChoose = resp.data.list;
                    }
                });
                this.loading = false;
            }else{
                this.shopListChoose = [];
            }
        },
        handleSelect(no) {
            this.shopListChoose.map((item, index) => {
                if(item.shop_no == no) {
                    this.ruleForm.shop_logo = item.shop_logo;
                }
            });
        },

        /**
         * 查看详情
         */
        getLookDetails (page) {
            this.$post('/student/shop_free%5Cget_day_stat',{
                page_id: page || this.page.now_page
            },resp=>{
                if (resp.code == 1) {
                    this.tableDataCash = this.pageArr(resp.data.list);
                    this.cashCount = resp.data.stat.total_money;
                    this.tableDateResh = this.tableDataCash[0];
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'warning',
                        message: resp.msg
                    });
                }
            });
        },

        /**
         * 分页
         */
        pageArr (arr) {
            let newArr = [];

            for (let i=0; i<arr.length; i+=10) {
                newArr.push(arr.slice(i,i+10));
            }

            return newArr;
        },

        /**
         * 刷新
         */
        fnRefresh () {
            let length = this.tableDataCash.length;//页数

            this.tableDateResh = this.tableDataCash[this.nowPage];
            this.nowPage ++;

            if (this.nowPage == length ) {
                this.nowPage = 0;
            }
        },

        /**
         * 设置起始库存
         */
        fnSetStock (row) {
            this.$prompt('为正整数数字', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /^([1-9]\d?|[1-9]\d+)$/,
                inputErrorMessage: '起始库存格式不正确,请重新输入'
            }).then(({ value }) => {
                this.$post('/student/shop_goods_flash_sale%5Cset_tao_item_stock',{
                    goods_no: row.goods_no,
                    stock: value
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$message({
                            type: 'success',
                            message: '设置成功',
                            center: true
                        });
                    } else if (resp.code == 0) {
                        this.$message({
                            type: 'warning',
                            message: resp.msg,
                            center: true
                        });
                    }
                });
            });
        },

        /**
         * 批量设置库存
         */
        fnBatchSetstock () {
            this.$prompt('为正整数数字', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /^([1-9]\d?|[1-9]\d+)$/,
                inputErrorMessage: '起始库存格式不正确,请重新输入'
            }).then(({ value }) => {
                this.multipleSelection.map(item=>{
                    this.$post('/student/shop_goods_flash_sale%5Cset_tao_item_stock',{
                        goods_no: item.goods_no,
                        stock: value
                    },resp=>{
                        if (resp.code == 1) {
                            this.indexSucess ++;
                        } else if (resp.code == 0) {
                            this.indexError ++;
                        }
                    });
                });

                //异常处理
                setTimeout(()=>{
                    if (this.indexError > 0) {
                        this.$message({
                            type: 'warning',
                            message: `设置失败个数${this.indexError}个,成功个数${this.indexSucess}个`,
                            center: true
                        });
                    } else {
                        this.$message({
                            type: 'success',
                            message: '设置成功',
                            center: true
                        });
                    }
                    this.indexSucess = this.indexError = 0;
                    this.getTableData();
                },100);
            });
        },

        /**
         * 查看详情
         */
        fnLookDetails () {
            this.dialogVisibleList = !this.dialogVisibleList;
        },

        /**
         * 移除
         */
        fnRemoveShop (row) {
            this.$confirm('此操作将移除该商品,是否继续？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            }).then((res) => {
                this.$post('/student/shop_goods_flash_sale%5Cdelete_tao_item',{
                    goods_no: row.goods_no
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$message({
                            type: 'success',
                            message: '移除成功'
                        });
                    } else if (resp.code == 0) {
                        this.$message({
                            type: 'warning',
                            message: resp.msg
                        });
                    }
                });
            }).catch((error) => {
                console.log(error,'error');
            });
        },

        /**
         * 批量移除
         */
        fnBatchRemove () {
            this.$confirm('此操作将批量移除选中商品, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.multipleSelection.map(item=>{
                    this.$post('/student/shop_goods_flash_sale%5Cdelete_tao_item',{
                        goods_no: item.goods_no
                    },resp=>{
                        if (resp.code == 1) {
                            this.indexSucess++;
                        } else if (resp.code == 0) {
                            this.indexError++;
                        }
                    });
                });

                setTimeout(()=>{
                    if (this.indexError > 0) {
                        this.$message({
                            type: 'warning',
                            message: `批量移除失败个数${this.indexError}`
                        });
                    } else {
                        this.$message({
                            type: 'success',
                            message: '操作成功'
                        });
                    }
                    this.indexError = this.indexSucess = 0;
                },500);
                setTimeout(()=>{
                    this.getTableData();
                },300);
            });
        },

        /**
         * 选择Checkbox
         */
        fnSelect(selection) {
            if(selection.length == this.tableData.length) {
                this.checkedAll = true;
            }else{
                this.checkedAll = false;
            }
            if(selection.length > 0 && selection.length < this.tableData.length) {
                this.checkIndeterminate = true;
            }else{
                this.checkIndeterminate = false;
            }
        },
        fnSelectAllTop(selection) {
            if(selection.length > 0) {
                this.checkedAll = true;
            }else{
                this.checkedAll = false;
            }
            if(selection.length > 0 && selection.length < this.tableData.length) {
                this.checkIndeterminate = true;
            }else{
                this.checkIndeterminate = false;
            }
        },
        fnChangeChecked() {
            if(this.checkedAll){
                this.toggleSelection(1);
            }else{
                this.toggleSelection();
            }
        },
        toggleSelection(rows) {
            if (rows) {
                this.$refs.zeroPurchase.toggleAllSelection();
            } else {
                this.$refs.zeroPurchase.clearSelection();
            }
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },

        /**
         * 取消排入秒杀
         */
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.seckillDialogVisible = !this.seckillDialogVisible;
        },

        /**
         * 排入秒杀
         */
        fnDischargeScekill () {
            let list = this.multipleSelection;

            for (let i=0; i<list.length; i++) {
                if (list[i].stock == '' || list[i].stock == 0) {
                    this.$message({
                        type: 'waring',
                        message: '没有设置库存的商品，不可排入秒杀',
                        center: true
                    });
                    return;
                }
            }

            this.seckillDialogVisible = !this.seckillDialogVisible;
        },

        /**
         * 排入秒杀日期选择
         */
        fnGetSckillDate () {
            let time = this.fnGeTtimeStamp(this.ruleFormMs.scekillData);

            if (time) {
                this.scekillTimeList = [];
                this.ruleFormMs.scekillTime = '';
                this.$post('/student/shop_goods_flash_sale%5Cget_simple_time_list', {
                    day_time: time / 1000
                }, resp=> {
                    if (resp.code == 1) {
                        if (resp.data.list.length == 0) {
                            this.$message({
                                type: 'waring',
                                message: '当前日期暂无排期'
                            });
                            return;
                        }
                        resp.data.list.map(item=>{
                            this.scekillTimeList.push({
                                value: item.time_no,
                                label: this.getHous(item.start_time) + ' - ' + this.getHous(item.end_time)
                            });
                        });
                    } else if (resp.code == 0) {
                        this.$message({
                            type: 'waring',
                            message: resp.msg
                        });
                    }
                });
            }
        },

        /**
         * 确定排入秒杀
         */
        submitFormMs(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.multipleSelection.map(item=> {
                        this.$post('/student/shop_goods_flash_sale%5Cadd', {
                            goods_no: item.goods_no,
                            time_no: this.ruleFormMs.scekillTime
                        }, resp=> {
                            if (resp.code == 1) {
                                this.indexSucess++;
                            } else if (resp.code == 0) {
                                this.indexError++;
                            }
                        });
                    });

                    this.seckillDialogVisible = !this.seckillDialogVisible;
                    this.getTableData();

                    setTimeout(()=>{
                        if (this.indexError > 0) {
                            this.$message({
                                type: 'waring',
                                message: `${this.indexError}个商品排期失败`
                            });
                        } else {
                            this.$message({
                                type: 'success',
                                message: '排期成功'
                            });
                        }
                        this.indexSucess = this.indexError = 0;
                    },500);
                } else {
                    return false;
                }
            });
        },

        /**
         * 获取选择日期时间戳
         */
        fnGeTtimeStamp (no) {
            let year = new Date(no).getFullYear(),
                month = new Date(no).getMonth()+1,
                day = new Date(no).getDate();
            let data = this.setDate(year) + '-' + this.setDate(month) + '-' + this.setDate(day);
            let time = new Date(data).getTime();

            return time;
        },

        /**
         * 时间戳转时+分
         */
        getHous (time) {
            let Hous = new Date(time * 1000).getHours(),
                Minus = new Date(time * 1000).getMinutes(),
                Sce = new Date(time * 1000).getSeconds();

            return this.setDate(Hous) + ':' + this.setDate(Minus) + ':' + this.setDate(Sce);
        },

        /**
         *  日期补0
         */
        setDate (param) {
            return param < 10 ? '0' + param : param;
        },

        /**
         * 排序
         */
        handleSortChanged(data) {
            let sortTypeMap = {
                ascending: 0,
                descending: 1
            };

            //  秒杀和淘礼金面额
            let sortFieldMap = {
                goods_price: 1,
                goods_coupon_money: 2,
                goods_price_pay: 3,
                goods_commission: 4,
                sale_num: 5,
                consume_money: 6,
                ms: 7,
                join_time: 0
            };

            if (data.order) {
                this.filterData.sort_field = sortFieldMap[data.prop];
                this.filterData.sort_type = sortTypeMap[data.order];
            } else {
                this.filterData.sort_field = '';
                this.filterData.sort_type = '';
            }

            this.getTableData();
        },

        /**
         * 设置表头
         */
        renderHeader(h){
            return h('span', {}, [
                h('span', {}, '淘宝售价'),
                h('el-popover', { props: { placement: 'top-start', width: '200', trigger: 'hover', content: '淘宝售价为当前该商品在淘宝上的在售价格' }}, [
                    h('i', { slot: 'reference', class:'el-icon-question'}, '')
                ])
            ]);
        },
        renderHeaderPay(h){
            return h('span', {}, [
                h('span', {}, '券后价'),
                h('el-popover', { props: { placement: 'top-start', width: '200', trigger: 'hover', content: '券后价为淘宝售价 - 优惠券价格' }}, [
                    h('i', { slot: 'reference', class:'el-icon-question'}, '')
                ])
            ]);
        },
        renderHeaderMs(h) {
            return h('span', {}, [
                h('span', {}, '秒杀价'),
                h('el-popover', { props: { placement: 'top-start', width: '200', trigger: 'hover', content: '秒杀价=券后价- 佣金' }}, [
                    h('i', { slot: 'reference', class:'el-icon-question'}, '')
                ])
            ]);
        },
        renderHeaderComin(h) {
            return h('span', {}, [
                h('span', {}, '淘礼金面额'),
                h('el-popover', { props: { placement: 'top-start', width: '200', trigger: 'hover', content: '淘礼金面额=佣金价格' }}, [
                    h('i', { slot: 'reference', class:'el-icon-question'}, '')
                ])
            ]);
        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 5px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            .header-box {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                .header-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
                .header-price {
                    padding: 0 5px;
                }
                .search-ipt {
                    width: 120px;
                }
                .btn-import-wrap {
                    margin-left: 15px;
                    display: inline-block;
                    position: relative;
                    overflow: hidden;
                    cursor: pointer;
                    vertical-align: top;
                    .import-input {
                        margin-left: -100%;
                        position: absolute;
                        left: 0;
                        top: 0;
                        width: 200%;
                        height: 100%;
                        cursor: pointer;
                        opacity: 0;
                    }
                }
            }
            .header-right {
                display: flex;
                .el-button {
                    margin-left: 20px;
                }
            }
        }
        .main {
            display: flex;
            margin-bottom: 20px;
            justify-content: space-between;
            height: 30px;
            align-items: center;
            &-icon {
                padding-right: 40px;
                font-size: 20px;
                cursor: pointer;
            }
        }
        .zd_cont {
            display: flex;
            align-items: center;
            margin: 0 0 15px 0;
            &-title {
                font-size: 15px;
                color: #333;
            }
            &-price {
                font-size: 14px;
                color: #333;
            }
            &-xq {
                font-size: 14px;
                color: #409EFF;
                padding-left: 60px;
                cursor: pointer;
            }
        }
        .coupon_num {
            margin-left: 50%;
            transform: translateX(-50%);
        }
        .couponBtn {
            width: 50%;
            display: flex;
            justify-content: space-evenly;
            margin-top: 60px;
            margin-left: 50%;
            transform: translateX(-50%);
        }
        .img-cover {
            width: 50px;
            height: 50px;
        }
        .curse-add {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        .pagination-wrapper {
            padding-top: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
        .dialog-footer {
            display: flex;
            justify-content: center;
            align-items: center;
        }
    }
    .el-dialogAdd {
        transition: all 1s;
    }
    .el-tag {
        margin: 0 15px 15px 0;
    }
    .sort-wrap {
        display: flex;
        align-items: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .switch-fixed {
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        right: 0;
        margin: auto;
        z-index: 999;
        cursor: pointer;
    }
    .scekillrel {
        margin-left: -30px;
        margin-top: 10px;
    }
    .scekillbtn {
        margin-left: 120px;
        margin-top: 10px;
    }
</style>
<style lang="less">
    .seckillTkShop .el-table__body-wrapper .el-table__body .el-table__row td {
        border-right: none;
    }
    .seckillTkShop .el-table__header-wrapper .el-table__header .has-gutter tr th {
        border-right: none;
    }
    .seckillTkShop .el-table .el-table_1_column_6  .cell {
        position: relative;
    }
    .seckillTkShop .el-range-editor--small .el-range-separator{
        padding: 0 1px !important;
    }
</style>